define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity/information/index' + location.search,
                    add_url: 'activity/information/add',
                    edit_url: 'activity/information/edit',
                    del_url: 'activity/information/del',
                    multi_url: 'activity/information/multi',
                    import_url: 'activity/information/import',
                    table: 'qsy_information_activity',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title')},
                        {field: 'cover_img', title: __('Cover_img'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'label', title: __('Label'), operate: 'LIKE'},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'day_num', title: __('Day_num'), operate: false},
                        {field: 'sold_num', title: __('Sold_num'), operate: false},
                        // {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'),
                            searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3'),"4":__('Status 4')},
                            formatter: Table.api.formatter.status
                        },
                        {field: 'audit_time', title: __('Audit_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {
                            field: "operate",
                            title: __("Operate"),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: "edit",
                                    title: __("编辑"),
                                    classname: "btn btn-xs btn-success btn-editone",
                                    icon: "fa fa-pencil",
                                    url: "activity/information/edit",
                                    visible: function (e) {
                                        if (0 == e.status) return !0
                                    },
                                    success: function (e, a) {
                                        return table.bootstrapTable("refresh"), !1
                                    },
                                    error: function (e, t) {
                                        return console.log(e, t), Layer.alert(t.msg), !1
                                    }
                                },
                                {
                                    name: "status",
                                    title: __("活动上架"),
                                    classname: "btn btn-xs btn-success btn-magic btn-ajax",
                                    icon: "fa fa-check",
                                    text: "上架",
                                    confirm: "确认上架？",
                                    url: "activity/information/status",
                                    visible: function (e) {
                                        if (3 == e.status || 2 == e.status ) return !0
                                    },
                                    success: function (e, a) {
                                        return table.bootstrapTable("refresh"), !1
                                    },
                                    error: function (e, t) {
                                        return console.log(e, t), Layer.alert(t.msg), !1
                                    }
                                },
                                {
                                    name: "status",
                                    title: __("活动下架"),
                                    classname: "btn btn-xs btn-danger btn-ajax",
                                    icon: "fa fa-times",
                                    text: "下架",
                                    confirm: "确认下架？",
                                    url: "activity/information/status",
                                    visible: function (e) {
                                        if (1 == e.status ) return !0
                                    },
                                    success: function (e, a) {
                                        return table.bootstrapTable("refresh"), !1
                                    },
                                    error: function (e, t) {
                                        return console.log(e, t), Layer.alert(t.msg), !1
                                    }
                                },
                                {
                                name: "detail",
                                title: __("详情"),
                                classname: "btn btn-xs btn-info btn-dialog",
                                icon: "fa fa-eye",
                                url: "activity/information/detail"
                            }],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
